import axios from 'axios'
import qs from 'qs'
// import router from './router/index'
// import store from './store/index'
// import * as types from './store/types'

//
axios.default.timeout = 5000
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'

const instance = axios.create({
  baseURL: 'http://gateway.isoftone.com/api',
  timeout: 3000
  // headers: {'Access-Control-Allow-Origin':'*'}
  // transformRequest: function (data) {
  //   data = qs.stringify(data);
  //   return data;
  // }
});

instance.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
instance.defaults.headers.common ['Authorization'] = 'Basic aGVyYTpoZXJhc2VjcmV0';

axios.interceptors.request.use = instance.interceptors.request.use
instance.interceptors.request.use(config => {
  if (localStorage.getItem('token')) {
    config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`
      .replace(/(^\")|(\"$)/g, '')
  }
  return config
}, err => {
  return Promise.reject(err)
})
// axios拦截响应
instance.interceptors.response.use(response => {
  return response
}, err => {
  return Promise.reject(err)
})

export default {
  // 用户注册
  userRegister (data) {
    return instance.post('/ims/user/register', data)
  },
  // 用户登录
  UserLogin (data) {
    return instance.post( '/uaa/oauth/token', qs.stringify(data),
      {'headers':
        { 'Content-Type' : 'application/x-www-form-urlencoded' }
      })
  },
  // 获取用户
  getUser () {
    return instance.get('/ims/user')
  },
  // 删除用户
  delUser (data) {
    return instance.post('/ims/delUser', data)
  }
}
